const {createUser, getUserInfo} = require('../service/user.server')
const {userAlreadyErr, databaseERR} = require("../errType/err.type");
const jwt = require("jsonwebtoken");

class UserController {
    async reguster(ctx, next) {
        const {user_name, password} = ctx.request.body
        //     操作数据库
        try {
            const res = await createUser(user_name, password)
            //     返回结果
            ctx.body = {
                code: 0, message: "用户注册成功", result: {
                    id: res.id, user_name: res.user_name
                }
            }
        } catch (err) {
            ctx.app.emit('err', databaseERR, err)
        }

    }

    async logo(ctx, next) {
        const {user_name, password} = ctx.request.body
        try {
            const {password, ...res} = await getUserInfo({user_name})
            // sin秘钥
            const my = "zddata"
            ctx.body = {
                code: 200,
                message: "登录成果",
                result: jwt.sign(res, my,
                    {
                        expiresIn: '1d'

                    })
            }
        } catch (err) {

        }
        // ctx.body = "用户登录成功"
    }
}

module.exports = new UserController();